<?php
namespace Admin\Controller;
use Think\Controller;
class teachcourseController extends Controller
{
    public function index()
    {
        $this->display();
       
    }

    /*权威大师课程信息*/
    public function teachcourselist(){
        $teachcourse = M('teachcourse');
        $teachcourselist = $teachcourse->order("tcid desc")->field('tcid,teachid,tcname,tcintro,tcvideo')->select();

        for($index=0;$index<count($teachcourselist);$index++)
        {
            $teachcourselist[$index]['number']=$index+1;
        }

        $this->assign('teachcourselist',$teachcourselist);
        $this->display();
    }
    
    /*权威大师课程修改*/
     public function editteachcourse()
{
        // $teachcourse=M('teachcourse');
        // $cid=$_GET['cid'];
        // $res = $teachcourse->where("cid=$cid")->select();      
        // $this->teachcourselist=$res;//name属性对应的值 $this->对应值=$find;
        // $this->display();
        $tcid=$_GET['tcid'];
        $teachcourseModel=M('teachcourse');//表明,conf中定义前缀
        $teachcourse = $teachcourseModel->where("tcid=$tcid")->select();

        //dump($lost);
        $this->teachcourse=$teachcourse;//name属性对应的值 $this->对应值=$find;
        $this->display();
    }

    public function update(){

            $data=$_POST;
            $file=$_FILES;
            
            $tcid=$data['tcid'];
                   
            $teachcourseModel = M('teachcourse'); // 实例化User模型
            // 要修改的数据对象属性赋值
         
        if ($file['tcpic']['size']==0) {
            //没有修改头像
            $result=$teachcourseModel->where("tcid=$tcid")->save($data);
            
         
  
        }else{
            //修改头像
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize  = 3145728 ;// 设置附件上传大小
            $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath  =  './';
            $upload->savePath =  'Public/Uploads/photo/';// 设置附件上传目录
            $upload->autoSub = false;
            $upload->saveName = date("YmdHis").rand(100,999);//定义名字
            $info = $upload->upload();
    
            if(!$info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
             }else{// 上传成功 获取上传文件信息
                 //保存表单数据 包括附件数据
                


                $data['tcpic']= $info['tcpic']['savename']; 
                $result=$teachcourseModel->where("tcid=$tcid")->save($data);
                    if ($data['oldpic']!='nopic.jpg') {
                    $oldpicPath='Public/Uploads/photo/'.$data['oldpic'];
                    unlink($oldpicPath);
                        }
                    }
            }
           
           if($result){
                $this->success('修改成功！','../teachcourse/teachcourselist');
                }
               else{
                   $this->error('修改失败！');
                   }
    } 



    /*权威大师课程添加*/
public function addteachcourse()
     
    {
        $this->display();
       
    }
        public function upload(){
        $m = M("teachcourse");
        $m->tcname=I("post.tcname");
        $m->tcintro=I("post.tcintro");
        $m->tcvideo=I("post.tcvideo");
        $m->teachid = $_SESSION['teachid'];
        if(false!=$m->add()){
            $this->redirect('teachcourse/teachcourselist','',2,'添加成功，页面跳转中...');
        }else{
            $this->error("添加失败！");
        }
    }
   
    /*权威大师课程删除*/
    public function deleteteachcourse()
    {
        $tcid=I('tcid');
        $teachcoursesModel = M('teachcourse');
        $teachcourse=$teachcoursesModel->where("tcid=$tcid")->find();
        if($teachcoursesModel->delete($id)) {
            $this->success('删除成功');
        }
        else {
            $this->error('删除失败');
        }
    }
}